﻿using System;
using System.Collections.Generic;
using Habanero.Base;
using Habanero.BO;
using Habanero.UI.Base;
using Habanero.UI.Win;
using UseCaseManager.BO;
using UseCaseManager.Logic;
using MessageBoxButtons = Habanero.UI.Base.MessageBoxButtons;
using MessageBoxIcon = Habanero.UI.Base.MessageBoxIcon;

namespace UseCaseManager.UI
{
    public class UseCaseCompletedSelectorControl : UserControlWin, IFormControl
    {
        public UseCaseCompletedSelectorControl()
        {
            CreateSelector();
        }

        public UseCaseCompletedSelectorControl(Drop drop)
        {
            if (drop == null) throw new ArgumentNullException("drop");
            CreateSelector();
            DropCompletedIn = drop;
        }

        protected BorderLayoutManager _layoutManager;
        public IMultiSelector<UseCase> UseCaseMultiSelector { get; private set; }
        private ButtonGroupControlWin _buttonGroupControlWin;

        /// <summary>
        /// Returns the SaveButton that is used to Save the Use Cases Selected as completed for the Drop.
        /// </summary>
        public IButton SaveButton { get; private set; }
        /// <summary>
        /// Returns the ExportCompletionStatusButton that is used to Export a table of drops vs. completed use cases 
        /// </summary>
        public IButton ExportCompletionStatusButton { get; private set; }

        private void CreateSelector()
        {
            UseCaseMultiSelector = GlobalUIRegistry.ControlFactory.CreateMultiSelector<UseCase>();
            _buttonGroupControlWin = new ButtonGroupControlWin(GlobalUIRegistry.ControlFactory);
            SaveButton = _buttonGroupControlWin.AddButton("Save", @"&Save", ClickHandlerSave);
            ExportCompletionStatusButton = _buttonGroupControlWin.AddButton("ExportCompletionStatus",
                                                                           @"Export &Completion Status",
                                                                           ClickHandlerExportCompletionStatus);
            _layoutManager = GlobalUIRegistry.ControlFactory.CreateBorderLayoutManager(this);
            _layoutManager.AddControl(UseCaseMultiSelector, BorderLayoutManager.Position.Centre);
            _layoutManager.AddControl(_buttonGroupControlWin, BorderLayoutManager.Position.South);
        }

        private void ClickHandlerSave(object sender, EventArgs e)
        {
            foreach (UseCase useCase in UseCaseMultiSelector.Model.Added)
            {
                useCase.CompletedInDropID = DropCompletedIn.DropID;
            }
            foreach (UseCase useCase in UseCaseMultiSelector.Model.Removed)
            {
                useCase.CompletedInDropID = null;
            }
            DropCompletedIn.UseCases.SaveAll();
            DropCompletedIn.Save();
            GlobalUIRegistry.ControlFactory.ShowMessageBox("Selected Options marked as completed", "Marked as Completed", MessageBoxButtons.OK, MessageBoxIcon.None);
        }

        private void ClickHandlerExportCompletionStatus(object sender, EventArgs e)
        {
            CreateAndExportTable.UglyGetCompletionStatusTable(_release);
        }

        private Drop _dropCompletedIn;
        /// <summary>
        /// Gets and sets the Drop that this Multiselector is marking completed use cases for. Populates the Multiselector with Available and Selected Options
        /// </summary>
        public Drop DropCompletedIn
        {
            get { return _dropCompletedIn; }
            set
            {
                _dropCompletedIn = value;
                if (_dropCompletedIn == null)
                {
                    UseCaseMultiSelector.AllOptions = new List<UseCase>();
                    UseCaseMultiSelector.SelectedOptions = new List<UseCase>();
                    return;
                }

                //gets all relevant drops: previous and current
                Criteria criteriaAllDropsUpToDrop = new Criteria("EndDate", Criteria.ComparisonOp.LessThanEqual, _dropCompletedIn.EndDate);
                BusinessObjectCollection<Drop> dropCol = Broker.GetBusinessObjectCollection<Drop>(criteriaAllDropsUpToDrop);

                //gets all use cases incomplete previously + completed in this drop
                List<UseCase> useCasesToBeConsidered = new List<UseCase>();


                foreach (Drop drop in dropCol)
                {
                    foreach (UseCase useCase in drop.UseCases.Clone())
                    {
                        if ((useCase.CompletedInDropID == null) || (useCase.CompletedInDropID.ToString().Equals(_dropCompletedIn.DropID.ToString())))
                        {
                            useCasesToBeConsidered.Add(useCase);
                        }

                    }
                }

                //gets all completed usecases in this drop
                Criteria criteriaCompletedInDrop = new Criteria("CompletedInDropID", Criteria.ComparisonOp.Equals, _dropCompletedIn.DropID);
                BusinessObjectCollection<UseCase> useCasesCompleted = Broker.GetBusinessObjectCollection<UseCase>(criteriaCompletedInDrop);

                UseCaseMultiSelector.AllOptions = useCasesToBeConsidered;
                UseCaseMultiSelector.SelectedOptions = useCasesCompleted;
            }
        }

        private Release _release;
        public Release Release
        {
            get { return _release; }
            set { _release = value; }
        }

        /// <summary>
        /// Called whenever the control is placed on a form by the menu system.  Put any initialisation
        /// code in here that must be run whenever the form is displayed.
        /// </summary>
        public void SetForm(IFormHabanero form)
        {
        }
    }
}
